<?php

namespace app\console\behavior;

use think\Controller;
use think\Db;
use think\Url;
use think\Request;
use think\Session;
use think\View;
use app\console\model\AuthGroup as AuthGroupModel;
use app\console\model\AuthRule as AuthRuleModel;
use app\common\trilateral\Classify as ClassifyAlias;
use think\Cache;
class Auth extends Controller
{

    public function run(&$params)
    {
        $MODULE_NAME = strtolower(Request::instance()->module());
        $CONTROLLER_NAME = strtolower(Request::instance()->controller());
        $ACTION_NAME = strtolower(Request::instance()->action());
        //
        $apply_count =Db::name('apply_log')->where('status',1)->count('id');
        $this->assign('apply_count',$apply_count);
//        $d = Cache::get('rule');
//
//        $ruless = [];
//        foreach ($d as $k => $v){
//            if($v['module'] == '控制台') continue;
//            $ruless[$v['name']][] = $v;
//        }
        $rules_list = Db::name('auth_group')->alias('a')->join('manager b','a.id = b.level')->where('b.id',session('manage_id'))->value('rules');
        $rule_data = Db::name('auth_rule')->where('id','in',$rules_list)->select()->toArray();
        //Cache::get('rule')

        if(\session('role_id') == 17){
            $rule_data = Db::name('auth_rule')->where('id','in',$rules_list)->where('name','neq','订单管理')->select()->toArray();
        }
//        dump($rule_data);
        $this->assign('pri_list',getMenuList($rule_data,0,'parenTid'));


        if ($CONTROLLER_NAME != 'login') {
            //if ($ACTION_NAME == 'index') {
                $auth = new \think\Auth();
                if($CONTROLLER_NAME == 'dashboard') return true;
                $result = $auth->check($MODULE_NAME . '/' . $CONTROLLER_NAME . '/' . $ACTION_NAME, Session::get('manage_id'));
               // halt($MODULE_NAME . '/' . $CONTROLLER_NAME . '/' . $ACTION_NAME);
               if (!$result){
                   if(Session::get('manage_id') == 1){
                       //halt('调试中...');
                       $array = ['salesman','orderlook','state','tenmin','excel','single_explain','auding_orderf','auding_orderq','use_coupon_service','car_number2','go','car_list_destory','car_list_create','car_list_update','car_list','status','ajax_goods_shop_price','ajax_goods_shop_price1','single_ajax_goods_shop_price1','servicepriceupdate1','ajax4a','ajax2a','single_ajax','destroy','shop_assignor_records','servicepriceupdate','now_gift_count','car_number','goods_sale','goods_shop_id','update_shop_id','erjilei','attrdel','attrflag','update_dayin','is_delete','take_tyre','cancel','a_pay','w_pay','news_gift_count','service_count_shop','goods_count_shop','news_gift','news_shop','refund_service_count','service_count','news','gift_count','gift_count','goods_count','refund_gift_count','refund_gift_news','refund_service_count','refund_news','refund_goods_count','refund_service_count','refund_goods_count'];
                   }else{
                       $array = ['orderlook','state','tenmin','excel','single_explain','auding_orderf','auding_orderq','use_coupon_service','car_number2','go','car_list_destory','car_list_create','car_list_update','car_list','status','ajax_goods_shop_price','ajax_goods_shop_price1','single_ajax_goods_shop_price1','servicepriceupdate1','ajax4a','ajax2a','single_ajax','destroy','shop_assignor_records','servicepriceupdate','now_gift_count','car_number','goods_shop_id','update_shop_id','erjilei','attrdel','attrflag','update_dayin','is_delete','take_tyre','cancel','a_pay','w_pay','news_gift_count','service_count_shop','goods_count_shop','news_gift','news_shop','refund_service_count','service_count','news','gift_count','gift_count','goods_count','refund_gift_count','refund_gift_news','refund_service_count','refund_news','refund_goods_count','refund_service_count','refund_goods_count'];
                   }


                   if(!in_array($ACTION_NAME,$array)){

                   auth_fail('没有权限', Url::build('console/dashboard/index'));
                   }else{

                   }
               }
            //}

        }
    }
}